<?php
    /**
    *@desc 封闭curl的调用接口，get的请求方式。
   */
   function doCurlGetRequest($url,$timeout = 5){
    if($url == "" || $timeout <= 0){
        return false;
    }
    $con = curl_init();
    curl_setopt($con, CURLOPT_URL, $url);
    curl_setopt($con, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($con, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($con, CURLOPT_HEADER, 0);
    curl_setopt($con, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($con);
    curl_close($con);
    $resultArr=json_decode($result,true);
    return $resultArr;
   }
   
    //获取唯一订单号
   function get_unique_no(){
        $order_number = date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        return $order_number;
   }


   //向普通http服务器POST数据
   function do_post($url = '', $param = '') {
    if (empty($url) || empty($param)) {
        return false;
    }

    $postUrl = $url;
    $curlPost = http_build_query($param);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$postUrl);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}


// 服务器推送的消息
function push_message($channel,$content){
    $url='http://rest-hangzhou.goeasy.io/publish';
    $post_arr=array(
        'appkey'=>C('GOEASY_APPKEY'),
        'channel'=>$channel,
        'content'=>json_encode($content)    
    );
    $res=do_post($url,$post_arr);
    return $res;
}

/**
 * 封装base64位图片上传
 */
function base64_upload($base64,$uploadPath='img') {
    $base64_image = str_replace(' ', '+', $base64);
    //post的数据里面，加号会被替换为空格，需要重新替换回来，如果不是post的数据，则注释掉这一行
    if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_image, $result)){
        //匹配成功
        if($result[2] == 'jpeg'){
            $image_name = uniqid().'.jpg';
            //纯粹是看jpeg不爽才替换的
        }else{
            $image_name = uniqid().'.'.$result[2];
        }
        $path="./Upload/" . $uploadPath;
        if (!file_exists($path)){
            mkdir($path); 
        }
        $image_file = "./Upload/". $uploadPath ."/" . $uploadPath . "_" . $image_name;
        //服务器文件存储路径
        if (file_put_contents($image_file, base64_decode(str_replace($result[1], '', $base64_image)))){
            return "/Upload/". $uploadPath . "/" . $uploadPath . "_" . $image_name;
        }else{
            return false;
        }
    }else{
        return false;
    }
}


// 生成订单号
function build_order_no() {    
    return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
}


// 获取基础token
function get_buse_token(){
    $weichart=M('weichart');
    $weichart_select_res=$weichart->where('id=1')->find();
    if($weichart_select_res){
        $start_data=$weichart_select_res['create_time'];
        $end_data=date('Y-m-d H:i:s',time());

        $time_res=start_end_time($start_data,$end_data);

        if($time_res<4200){
            return $weichart_select_res['access_token'];
        }else{
            $access_token=get_access_token();
            $save_data=array(
                'id'=>1,
                'access_token'=>$access_token,
                'create_time'=>date('Y-m-d H:i:s',time())
            );
            $weichart->save($save_data);
            return $access_token;
        }
    }else{
        $access_token=get_access_token();
        $save_data=array(
            'access_token'=>$access_token,
            'create_time'=>date('Y-m-d H:i:s',time())
        );
        $weichart->data($save_data)->add();     
        return $access_token;   
    }
}

// 请求access_token
function get_access_token(){
    $WEIXIN_CONFIG=C('WCHART_CONFIG');
    $url='https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid='.$WEIXIN_CONFIG["APPID"].'&secret='.$WEIXIN_CONFIG["SERECT"];
    $get_res=doCurlGetRequest($url,30);
    return $get_res['access_token'];
}

// 计算时间差
function start_end_time($startdate="2011-3-15 11:50:00",$enddate="2012-12-12 12:12:12"){
    $second=floor((strtotime($enddate)-strtotime($startdate)));
    return $second;
}



// 检查admin登录状态
function check_admin_login(){
    $PHPSESION = cookie('PHPSESSID');
    $admin_uname=session('admin_uname');

    $login_check_res=M('onlyone_login')->where(array('user_name'=>$admin_uname,'cookie'=>$PHPSESION))->select();
    
    if($login_check_res){
        return true;
    }else{
        return false;
    }
    
}


?>